package com.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.business.domain.CarModelVo;
import com.business.domain.LoanScheme;
import com.business.domain.LoanVo;
import com.sys.Utils.DbConstants;
import com.sys.dao.DictItemMapper;
import com.sys.domain.DictItem;
import com.sys.domain.DictItemExample;
import com.sys.domain.DictItemExample.Criteria;
import com.web.service.CarLoanPlanService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/web/loanPlan")
public class CarLoanPlanController {

    @Resource
    private CarLoanPlanService carLoanPlanService;

    @Resource
    private DictItemMapper dictItemMapper;

    @RequestMapping("toLoanPlan")
    public String toLoanPlan(Model model, Integer rental, String brand, Double payment, Integer installment) {
        model.addAttribute("rental", rental);
        model.addAttribute("brand", brand);
        model.addAttribute("payment", payment);
        model.addAttribute("installment", installment);

        DictItemExample example = new DictItemExample();
        example.setOrderByClause("item_order");
        Criteria Criteria = example.createCriteria();
        Criteria.andDictCodeEqualTo(DbConstants.LOAN_PERIOD);
        List<DictItem> dictItemList = dictItemMapper.selectByExample(example);
        model.addAttribute("dictItemList", dictItemList);

        return "car/loanPlan";
    }

    @RequestMapping("/getLoanPlan")
    public @ResponseBody
    Object getAllLoanPlan(CarModelVo modelVo, LoanVo loanVo, Double chejia) {
        List<LoanScheme> loanPlan = carLoanPlanService.getAllLoanPlan(modelVo, loanVo, chejia);
        return JSONArray.toJSON(loanPlan);
    }

    @RequestMapping("/getAllLoanPlan")
    public String getLoanPlan(Model model, CarModelVo modelVo, LoanVo loanVo, String type, Double chejia) {
        List<LoanScheme> loanPlan = carLoanPlanService.getLoanPlan(modelVo, loanVo, type, chejia);
        String str = "1".equals(type) ? "最省钱贷款" : ("2".equals(type) ? "最快速贷款" : "最便捷贷款");
        model.addAttribute("title", str);
        model.addAttribute("installment", loanVo.getInstallment());
        model.addAttribute("list", loanPlan);
        return "car/loanPlanItem";
    }
}
